<!DOCTYPE html>
<html>
<head>
<title>Test attribute removing to check attributeChanged callback of a custom element</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="assert" content="attributeChanged callback must be enqueued whenever custom element's attribute is removed">
<link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<link rel="stylesheet" href="/resources/testharness.css">
</head>
<body>
<div id="log"></div>
<script>
test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    var GeneratedConstructor = doc.registerElement('x-a', {prototype: proto});

    var customElement = new GeneratedConstructor();
    //attributeChangedCallback should be called the first time here
    customElement.setAttribute('class', 'someClass');
    //attributeChangedCallback should be called the second time here
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 'Callback attributeChanged should be called ' +
        'after setAttribute() and removeAttribute() calls');
}, 'Test attributeChanged callback if attribute is removed. ' +
    'The custom element created via constructor');


test(function() {
    var doc = newHTMLDocument();
    HTML5_ELEMENTS.forEach(function(element) {
        var obj = doc.createElement(element);
        var proto = newCustomElementPrototype(obj.constructor.prototype);
        var GeneratedConstructor = doc.registerElement('x-' + element + '-' + element + '-1', {
            prototype: proto,
            extends: element
        });

        var customElement = new GeneratedConstructor();
        //attributeChangedCallback should be called the first time here
        customElement.setAttribute('class', 'someClass');
        //attributeChangedCallback should be called the second time here
        customElement.removeAttribute('class');
        assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 
            'Callback attributeChanged should be called ' +
            'after setAttribute() and removeAttribute() calls for "' + element + '"');
    });
}, 'Test attributeChanged callback if attribute is removed. ' +
    'The custom element created via constructor and extends HTML element');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-b', {prototype: proto});

    doc.body.innerHTML = '<x-b id="x-b" class="theClass"></x-b>';
    var customElement = doc.querySelector('#x-b');
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 1, 
        'Callback attributeChanged should be called ' +
        'after removeAttribute() call');
}, 'Test attributeChanged callback if attribute is removed. ' +
    'The custom element created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-c', {prototype: proto});

    doc.body.innerHTML = '<x-c id="x-c" class="theClass"></x-c>';
    var customElement = doc.querySelector('#x-c');
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackArgs[2], null, 
        'Removing an attribute should invoke ' +
        'the attributeChanged callback with a null new value');
    assert_array_equals(proto.attributeChangedCallbackArgs, 
        ['class', 'theClass', null],
        'Unexpected attributeChanged callback arguments');
}, 'Test attributeChanged callback arguments if attribute is removed. ' +
    'The custom element created via innerHTML property');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.body.innerHTML = '<x-d id="x-d" class="theClass"></x-d>';
    var customElement = doc.querySelector('#x-d');
    // this should not call or enqueue attributeChangedCallback
    customElement.setAttribute('class', 'someClass');
    // this one should not too
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 0, 
        'Callback attributeChanged should not be called');

    doc.registerElement('x-d', {prototype: proto});
    // this call invokes attributeChangedCallback
    customElement.setAttribute('name', 'someName');
    // and this one
    customElement.removeAttribute('name');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 
        'Callback attributeChanged should be called ' +
        'after setAttribute() and removeAttribute() calls');
}, 'Test attributeChanged callback is not called if attribute is removed. ' +
    'The custom element created via innerHTML property and unresolved at first');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    var customElement = doc.querySelector('#x-element');
    customElement.setAttribute('class', 'someClass');
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 
        'Callback attributeChanged should be called ' +
        'after setAttribute() and removeAttribute() calls');
}, 'Test attributeChanged callback is called if attribute is removed. ' +
    'The custom element created via constructor and the document has browsing context');


testInIFrame('../../resources/x-element.html', function(doc) {
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});

    doc.body.innerHTML = '<x-element id="x-element" class="theClass"></x-element>';
    var customElement = doc.querySelector('#x-element');
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackArgs[2], null, 
        'Removing an attribute should invoke ' +
        'the attributeChanged callback with a null new value');
    assert_array_equals(proto.attributeChangedCallbackArgs, 
        ['class', 'theClass', null],
        'Unexpected attributeChanged callback arguments');
}, 'Test attributeChanged callback arguments if attribute is removed. ' +
    'The custom element created via innerHTML property and the document has browsing context');


testInIFrame('../../resources/x-element.html', function(doc) {
    var customElement = doc.querySelector('#x-element');
    // this should not call or enqueue attributeChangedCallback
    customElement.setAttribute('name', 'someName');
    // this one too
    customElement.removeAttribute('name');

    var proto = newHTMLElementPrototype();
    doc.registerElement('x-element', {prototype: proto});
    assert_equals(proto.attributeChangedCallbackCalledCounter, 0, 
        'Callback attributeChanged should not be called');
    // this call invokes attributeChangedCallback
    customElement.setAttribute('class', 'someClass');
    // this call invokes attributeChangedCallback at second time
    customElement.removeAttribute('class');
    assert_equals(proto.attributeChangedCallbackCalledCounter, 2, 
        'Callback attributeChanged should be called ' +
        'after setAttribute() and removeAttribute() calls');
}, 'Test attributeChanged callback if attribute is removed. ' +
    'The custom element created via innerHTML property and unresolved at first. ' +
    'The document has browsing context');
</script>
</body>
</html>
